package com.bykj.taketurns.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.PersonalActivity;
import com.bykj.taketurns.activity.PlayVideoActivity;
import com.bykj.taketurns.activity.YWRecordVideoActivity;
import com.bykj.taketurns.model.VideoShowBean;
import com.dou361.ijkplayer.listener.OnPlayerBackListener;
import com.dou361.ijkplayer.listener.OnShowThumbnailListener;
import com.dou361.ijkplayer.widget.PlayStateParams;
import com.dou361.ijkplayer.widget.PlayerView;

/**
 * Created by carolina on 2017/7/20.
 * 视频播放页面
 *
 */

public class PlayVideoFragment extends Fragment {
    private PlayVideoActivity activity;
    private PlayerView player;
    private VideoShowBean video;
    private View view;
    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.simple_player_view_player,null);
        return view;
    }
    //绑定activity
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity= (PlayVideoActivity) context;
    }
    public void setData(String url, final String img, View view){
        player=new PlayerView(getActivity(),view)
                .setScaleType(PlayStateParams.f16_9)
                .forbidTouch(false)
                .hideMenu(false)
                .setPlayerRotation(0)
                .showThumbnail(
                        new OnShowThumbnailListener() {
                            @Override
                            public void onShowThumbnail(ImageView ivThumbnail) {
                                Glide.with(getActivity())
                                        .load(img)
                                        .into(ivThumbnail);
                            }
                        })
                .setPlaySource(url)
                .setAttention(false)
                .setPlayerBackListener(new OnPlayerBackListener() {
                    @Override
                    public void onPlayerBack() {
                        //这里可以简单播放器点击返回键
                        getActivity().finish();
                    }
                })
                .intentActivity(YWRecordVideoActivity.class)
                .intentActivitythi(PersonalActivity.class)
                .startPlay();

    }


    //获取焦点时添加
    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        if(bundle!=null){
            VideoShowBean video= (VideoShowBean) bundle.getSerializable("video");
            setData(video.getVideoUrl(),video.getPictureUrl(),view);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        player.onDestroy();

    }

    @Override
    public void onPause() {
        super.onPause();
        player.stopPlay();
    }

}
